package cn.miao.test.hello;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.nio.charset.StandardCharsets;

/**
 * @program: QuickStart-RabbitMQ
 * @description: 生产者
 * @author: MiaoWei
 * @create: 2021-11-12 14:08
 **/
public class Producer {
    public static void main(String[] args) throws Exception {
        //1.创建连接工程
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin");
        //设置虚拟访问节点,是指把我们的消息发在根节点上
        connectionFactory.setVirtualHost("/");

        //2.创建连接Connection
        Connection connection = connectionFactory.newConnection("生产者");
        //3.通过连接获取通道channel
        Channel channel = connection.createChannel();

        String queueName = "queue1";
        String message = "Hello MQ,I is Simple";

        //4.声明队列,这里就是创建队列,但是我们已经在图形化界面已经创建了所以这里就可以不用创建了
        //参数:1:队列名 2:是否持久化 3:是否独占队列 4:是否自动删除 5:其他参数
        channel.queueDeclare(queueName, true, false, false, null);
        //5.发送消息给队列queue,
        //参数: 1:交换机 2:队列名 3:其他参数 4:消息内容
        channel.basicPublish("", queueName, null, message.getBytes(StandardCharsets.UTF_8));
        System.out.println("消息发送成功!");
    }



}
